package com.example.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.auth.mapper.SysDepartmentMapper;
import com.example.auth.model.entity.SysDepartment;
import com.example.auth.model.vo.DepartmentTreeVo;
import com.example.auth.service.SysDepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门服务实现类
 */
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements SysDepartmentService {

    @Override
    public List<DepartmentTreeVo> getDepartmentTree() {
        // 获取所有部门
        LambdaQueryWrapper<SysDepartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysDepartment::getSort);
        List<SysDepartment> allDepartments = this.list(queryWrapper);
        
        // 将部门按父ID分组
        Map<Long, List<SysDepartment>> deptMap = allDepartments.stream()
                .collect(Collectors.groupingBy(SysDepartment::getParentId));
        
        // 构建树形结构
        List<DepartmentTreeVo> rootNodes = buildDeptTree(deptMap, 0L);
        return rootNodes;
    }

    @Override
    public List<SysDepartment> getDepartmentWithChildren(Long departmentId) {
        List<SysDepartment> result = new ArrayList<>();
        
        // 获取指定部门
        SysDepartment department = this.getById(departmentId);
        if (department != null) {
            result.add(department);
            
            // 递归获取子部门
            findChildren(departmentId, result);
        }
        
        return result;
    }
    
    /**
     * 递归构建部门树
     */
    private List<DepartmentTreeVo> buildDeptTree(Map<Long, List<SysDepartment>> deptMap, Long parentId) {
        List<DepartmentTreeVo> nodeList = new ArrayList<>();
        
        // 获取当前parentId下的所有部门
        List<SysDepartment> departments = deptMap.get(parentId);
        if (departments != null) {
            departments.forEach(dept -> {
                DepartmentTreeVo node = new DepartmentTreeVo();
                BeanUtils.copyProperties(dept, node);
                
                // 递归获取子部门
                node.setChildren(buildDeptTree(deptMap, dept.getId()));
                
                nodeList.add(node);
            });
        }
        
        // 按照sort排序
        nodeList.sort(Comparator.comparing(DepartmentTreeVo::getSort));
        
        return nodeList;
    }
    
    /**
     * 递归获取子部门
     */
    private void findChildren(Long parentId, List<SysDepartment> result) {
        LambdaQueryWrapper<SysDepartment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDepartment::getParentId, parentId);
        queryWrapper.orderByAsc(SysDepartment::getSort);
        
        List<SysDepartment> children = this.list(queryWrapper);
        if (children != null && !children.isEmpty()) {
            result.addAll(children);
            
            // 递归查找每个子部门的子部门
            children.forEach(child -> findChildren(child.getId(), result));
        }
    }
} 